﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussiness;

public partial class BrowserThemalTemplate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindExprimentName();
            Bussiness.ThermalTestReport report = new Bussiness.ThermalTestReport();
            DataTable dt = report.GetFinishExpriment();
            this.grvFeeInfo.DataSource = dt;
            this.grvFeeInfo.DataBind();

            if (this.ddlExperiment.SelectedValue == "0")
            {
                for (int i = 0; i < grvFeeInfo.Rows.Count; i++)
                {
                    GridViewRow row = grvFeeInfo.Rows[i];
                    CheckBox isChecked = ((CheckBox)row.FindControl("ChkSelected"));
                    isChecked.Enabled = false;
                }
            }
            else
            {
                for (int i = 0; i < grvFeeInfo.Rows.Count; i++)
                {
                    GridViewRow row = grvFeeInfo.Rows[i];
                    CheckBox isChecked = ((CheckBox)row.FindControl("ChkSelected"));
                    isChecked.Enabled = true;
                }
            }
        }
    }
    protected void grvFeeInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvFeeInfo.PageIndex = e.NewPageIndex;
        btnSearch_Click(null, null);
        ddlExperiment_SelectedIndexChanged(null, null);
    }
    protected void btnDisplay_Click(object sender, EventArgs e)
    {
        if (this.ddlExperiment.SelectedValue == "0")
        {
            Response.Write("<script language='javascript'> alert('不同类型实验无法进行对比，不能选择全部实验'); </script>");
            return;
        }

        string colReportId = "";
        string colReportName = "";
        for (int i = 0; i < grvFeeInfo.Rows.Count; i++)
        {
            GridViewRow row = grvFeeInfo.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("ChkSelected")).Checked;
            if (isChecked)
            {
                colReportId += this.grvFeeInfo.DataKeys[i].Value.ToString() + ',';
                colReportName += this.grvFeeInfo.Rows[i].Cells[2].Text + ',';
            }
        }

        if (colReportId != "")
        {
            colReportId = colReportId.Substring(0, colReportId.Length - 1);
            colReportName = colReportName.Substring(0, colReportName.Length - 1);
            //Response.Write(string.Format("<script language='javascript' > window.open('DisplayReport.aspx?ReportID={0}&ReportName={1}') </script>", colReportId,colReportName));
            Response.Write(string.Format("<script language='javascript' > window.open('../DisplayReport.aspx?ReportID={0}') </script>", colReportId));
        }


    }

    //
    private void BindExprimentName()
    {
        Bussiness.ThermalTestTemplate template = new Bussiness.ThermalTestTemplate();
        template.UnitID = this.ddlCompute.SelectedValue;
        DataTable dt = template.GetReportName();
        this.ddlExperiment.DataSource = dt;
        this.ddlExperiment.DataTextField = "TemplateName";
        //this.ddlExperiment.DataValueField = "TemplateID";
        this.ddlExperiment.DataBind();
        this.ddlExperiment.Items.Insert(0, new ListItem("全部", "0"));
    }

    protected void ddlCompute_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindExprimentName();
        ddlExperiment_SelectedIndexChanged(null, null);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bussiness.ThermalTestReport report = new Bussiness.ThermalTestReport();
        //DataTable dt = report.GetFinishExpriment(this.calStartTime.Text, this.calEndTime.Text, Convert.ToInt32(this.ddlExperiment.SelectedValue));
        //DataTable dt = report.GetExpriment(this.calStartTime.Text, this.calEndTime.Text, Convert.ToInt32(this.ddlExperiment.SelectedValue));
        //this.grvFeeInfo.DataSource = dt;
        //this.grvFeeInfo.DataBind();
        Bussiness.ThermalTestTemplate TestTemplate = new Bussiness.ThermalTestTemplate();
        //int templateId = TestTemplate.GetTemplateID(this.ddlExperiment.SelectedValue, this.ddlCompute.SelectedValue);

        DataTable dt = null;
        if (this.ddlExperiment.SelectedValue != "0")
        {
            dt = TestTemplate.GetTemplateIDs(this.ddlExperiment.SelectedValue, this.ddlCompute.SelectedValue);
            string templateId = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    templateId += dr[0].ToString() + ",";
                }

            }
            templateId = templateId.Substring(0, templateId.Length - 1);

            BindReport(this.calStartTime.Text, this.calEndTime.Text, templateId);
        }
        else
            BindReport(this.calStartTime.Text, this.calEndTime.Text, "0");


        //BindReport(this.calStartTime.Text, this.calEndTime.Text, templateId);   
       
    }
    //
    private void BindReport(string startTime,string endTime,string id_key)
    {
        Bussiness.ThermalTestReport report = new Bussiness.ThermalTestReport();
        
        DataTable dt = report.GetExpriment(startTime, endTime, id_key);
        this.grvFeeInfo.DataSource = dt;
        this.grvFeeInfo.DataBind();
    }

    protected void ddlExperiment_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bussiness.ThermalTestTemplate TestTemplate = new Bussiness.ThermalTestTemplate();
        DataTable dt = null;
        if (this.ddlExperiment.SelectedValue != "0")
        {
            dt = TestTemplate.GetTemplateIDs(this.ddlExperiment.SelectedValue, this.ddlCompute.SelectedValue);
            string templateId = "";
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    templateId += dr[0].ToString() + ",";
                }

            }
            templateId = templateId.Substring(0, templateId.Length - 1);

            BindReport(this.calStartTime.Text, this.calEndTime.Text, templateId);
        }
        else
            BindReport(this.calStartTime.Text, this.calEndTime.Text, "0");

        if (this.ddlExperiment.SelectedValue == "0")
        {
            for (int i = 0; i < grvFeeInfo.Rows.Count; i++)
            {
                GridViewRow row = grvFeeInfo.Rows[i];
                CheckBox isChecked = ((CheckBox)row.FindControl("ChkSelected"));
                isChecked.Enabled = false;
            }
        }
        else
        {
            for (int i = 0; i < grvFeeInfo.Rows.Count; i++)
            {
                GridViewRow row = grvFeeInfo.Rows[i];
                CheckBox isChecked = ((CheckBox)row.FindControl("ChkSelected"));
                isChecked.Enabled = true;
            }
        }
        
    }
}
